#!/usr/bin/env python
# -*- coding: utf8 -*-

'''
Created on 2011-7-26

京东价格图片识别模块

@author: zhongfeng
'''

import ImageFilter, ImageChops
from captcha_price import *
from medicine.kxr.kxr_feature import KXR_FEATURES_MAP__
import Image
import os,sys

try:
    import psyco
    psyco.full()
except ImportError:
    pass

class CaptchaProfile_kxr(CaptchaProfile):
    
    def __init__(self,features_map = KXR_FEATURES_MAP__):
        super(CaptchaProfile_kxr,self).__init__(features_map)
        
    def __new__(cls,features_map = KXR_FEATURES_MAP__):
        return super(CaptchaProfile_kxr, cls).__new__(cls,features_map)

def getStandardArrNum():
    curPath = os.path.dirname(sys.argv[0])
    imStand = os.path.join(curPath,'price2.png')
    im = Image.open(imStand)
    #print im.size
    #cia = CaptchaImageAlgorithm()
    cpKxr = CaptchaProfile_kxr()
    ckDict = {}
    for i in xrange(10):
        for j in xrange(11):
            xt = 14*(j+1)
            if xt > 146:
                xt = 146
            px = (14*j,16 + (i-1)*15,xt,15 + i*15)
            seg = im.crop(px)
            ckDict[(px[0],px[1] - 1)] = cpKxr.match(seg)
    return ckDict

class CkDict(object):
    ckDict = None
    @classmethod
    def getCapNum(cls,px):
        if cls.ckDict is None:
            cls.ckDict = getStandardArrNum()
        return cls.ckDict.get(px,None)
        

if __name__ == '__main__':
    while True:
        getStandardArrNum()
    #===========================================================================
    # im = Image.open(r'c:\1.png')
    # im2 = Image.open(r'c:\1.png')
    # dt = im.getdata()
    # print im.size
    # it1 = im.crop((15, 3, 21, 11))
    # it2 = im.crop((23, 3, 29, 11))
    # it3 = im.crop((31, 3, 37, 11))
    # it4 = im.crop((39, 3, 45, 11))
    # it5 = im.crop((47, 3, 49, 11))
    # it6 = im.crop((51, 3, 57, 11))
    # it7 = im.crop((59, 3, 65, 11))
    # cia = CaptchaImageAlgorithm()
    # s7 = cia.GetBinaryMap(it1)
    # print s7
    #===========================================================================
        
        
